#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
#endregion

namespace WindowsGame1.Components
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class FrameRate : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public FrameRate(Game game)
            : base(game)
        {
        }


        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            timeElapsed = new TimeSpan();
                lastframerate = framerate;
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            base.Update(gameTime);
            framerate++;
            if (timeElapsed.TotalSeconds >= 1)
            {
                lastframerate = framerate;
                timeElapsed = timeElapsed.Subtract(new TimeSpan(0, 0, 1));
                framerate = 0;
            }
            timeElapsed = timeElapsed.Add(gameTime.ElapsedGameTime);
        }

        int framerate = 0;
        int lastframerate = 0;
        TimeSpan timeElapsed;
        private SpriteFont font;
        private SpriteBatch sprite;

        protected override void LoadContent()
        {
            base.LoadContent();
            ContentManager content = new ContentManager(Game.Services);
            sprite = new SpriteBatch(GraphicsDevice);
            font = content.Load<SpriteFont>(SpriteFontAssetName);
        }

        public string SpriteFontAssetName { get; set; }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            sprite.Begin();
            sprite.DrawString(font, String.Format("fps: {0}", lastframerate), font.MeasureString(String.Format("fps: ", lastframerate)) / 2, Color.LightGreen);
            sprite.End();
        }
    }
}


